/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.base;

import javafx.geometry.Rectangle2D;
import javafx.scene.input.MouseEvent;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 * 基础控制器
 * @author zpy
 * @version 1.0.0
 */
public abstract class BaseWindow {
    protected final static int DOUBLE_CLICK = 2;
    private final static double MIN_WIDTH = 900.00;
    private final static double MIN_HEIGHT = 500.00;
    protected boolean maximized;
    protected double initWidth;
    protected double initHeight;
    protected double initStageX;
    protected double initStageY;
    protected double xOffset = 0, yOffset = 0;

    protected void windowMax(Stage stage){
        this.maximized = !this.maximized;
        if(this.maximized){
            //最大化
            this.initStageX = stage.getX();
            this.initStageY = stage.getY();
            this.initWidth = stage.getWidth();
            this.initHeight = stage.getHeight();
            Rectangle2D rectangle2D = Screen.getPrimary().getVisualBounds();
            stage.setX(rectangle2D.getMinX());
            stage.setY(rectangle2D.getMinY());
            stage.setWidth(rectangle2D.getWidth());
            stage.setHeight(rectangle2D.getHeight());
        }else{
            //还原
            stage.setX(initStageX);
            stage.setY(initStageY);
            stage.setWidth(initWidth);
            stage.setHeight(initHeight);
        }
    }

    protected void onMouseDragged(MouseEvent event, Stage stage){
        //根据鼠标的横纵坐标移动dialog位置
        event.consume();
        if (yOffset != 0 ) {
            stage.setX(event.getScreenX() - xOffset);
            if (event.getScreenY() - yOffset < 0) {
                stage.setY(0);
            } else {
                stage.setY(event.getScreenY() - yOffset);
            }
        }

        // 保存窗口改变后的x、y坐标和宽度、高度，用于预判是否会小于最小宽度、最小高度
        double nextX = stage.getX();
        double nextY = stage.getY();
        double nextWidth = stage.getWidth();
        double nextHeight = stage.getHeight();
        if (nextWidth <= MIN_WIDTH) {
            nextWidth = MIN_WIDTH;
        }
        if (nextHeight <= MIN_HEIGHT) {
            nextHeight = MIN_HEIGHT;
        }
        // 最后统一改变窗口的x、y坐标和宽度、高度，可以防止刷新频繁出现的屏闪情况
        stage.setX(nextX);
        stage.setY(nextY);
        stage.setWidth(nextWidth);
        stage.setHeight(nextHeight);
    }

    protected void onMousePressed(MouseEvent event){
        event.consume();
        xOffset = event.getSceneX();
        yOffset = event.getSceneY();
    }
}